/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Newgame;

import Global.ConnectDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrator
 */
public class Highscore extends javax.swing.JFrame {

    /**
     * Creates new form Highscore
     */
    ConnectDB connectDB;
    CallableStatement cst, cst2;
    ResultSet rs, rs2;
    int flag = 0;
    int ct = 1;
    DefaultTableModel model;
    DefaultComboBoxModel modelcbb;
    public Highscore() {
        initComponents();
        model = (DefaultTableModel)tbTrinh.getModel();
        cst = null;
        rs = null;
        connectDB = new ConnectDB("1433","localhost","mssqlserver","eproject1","sa","123123");
        connectDB.openConnection();
        
        String name = "{call getAllName}";
        try{
            cst = connectDB.cn.prepareCall(name, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
            rs = cst.executeQuery();
            
        }catch(SQLException ex){
            ex.printStackTrace();
        }
        
        loadDataIntoTable();
    }
    
        public void loadDataIntoTable() {
        ResultSet rsloadtotable;
        model.setRowCount(0);
        try {
            String proc = "{call getHighscores}";
            cst = connectDB.cn.prepareCall(proc, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rsloadtotable = cst.executeQuery();
            while (rsloadtotable.next()) {
                model.addRow(new String[]{ rsloadtotable.getString(2), String.valueOf(rsloadtotable.getInt(3)), String.valueOf(rsloadtotable.getInt(4))});
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbTrinh = new javax.swing.JTable();
        OK = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(430, 550));
        setPreferredSize(new java.awt.Dimension(430, 550));
        setResizable(false);
        getContentPane().setLayout(null);

        tbTrinh.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Name", "Scores", "Times"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tbTrinh);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(20, 130, 380, 360);

        OK.setText("OK");
        OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKActionPerformed(evt);
            }
        });
        getContentPane().add(OK);
        OK.setBounds(170, 490, 50, 23);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/images.jpg"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 420, 520);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKActionPerformed
        this.dispose();
        new NewgameOpt().setVisible(true);
    }//GEN-LAST:event_OKActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Highscore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Highscore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Highscore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Highscore.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Highscore().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton OK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbTrinh;
    // End of variables declaration//GEN-END:variables
}
